package tv.huan.tencent.rabbit.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import tv.huan.media.domain.Album;
import tv.huan.media.mail.MailUtil;
import tv.huan.media.rabbit.RabbitConstant;
import tv.huan.media.util.MailInfoUtil;

import java.io.IOException;
import java.util.List;


@Component
@RabbitListener(queues = RabbitConstant.CARD_POOL_EMAIL_QUEUE)
public class CardPoolEmailConsumer {

    private static final Logger log = LoggerFactory.getLogger(CardPoolEmailConsumer.class);

    @Autowired
    private MailUtil mailUtil;

    /**
     * 接收方邮箱
     */
    @Value("${spring.mail.tencentCardPoolTo}")
    private String tencentCardPoolTo;

    @RabbitHandler
    public void process(String json, Channel channel, Message message)throws IOException {
        try {
            JSONObject jsonObject = JSON.parseObject(json);
            String subject = jsonObject.getString("subject");
            String content = jsonObject.getString("content");
            //log邮件主题:::::参数
            log.info(subject + "::::" + json);
            log.info("邮件接收人：" + tencentCardPoolTo);
            String[] toArray = tencentCardPoolTo.split(",");
            mailUtil.sendMailHtml(subject, content, toArray);
            //消息确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            //失败后消息被确认
//            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            log.error("mq接收或处理消息失败",e);
        }
    }
}
